package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.DatabaseBackup;
import com.ruoyi.system.service.IDatabaseBackupService;

/**
 * 数据库备份管理控制器
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/backup")
public class DatabaseBackupController extends BaseController
{
    @Autowired
    private IDatabaseBackupService databaseBackupService;

    /**
     * 查询数据库备份列表
     */
    @PreAuthorize("@ss.hasPermi('system:backup:list')")
    @GetMapping("/list")
    public TableDataInfo list(DatabaseBackup databaseBackup)
    {
        startPage();
        List<DatabaseBackup> list = databaseBackupService.selectDatabaseBackupList(databaseBackup);
        return getDataTable(list);
    }

    /**
     * 导出数据库备份列表
     */
    @PreAuthorize("@ss.hasPermi('system:backup:export')")
    @Log(title = "数据库备份", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DatabaseBackup databaseBackup)
    {
        List<DatabaseBackup> list = databaseBackupService.selectDatabaseBackupList(databaseBackup);
        ExcelUtil<DatabaseBackup> util = new ExcelUtil<DatabaseBackup>(DatabaseBackup.class);
        return util.exportExcel(list, "数据库备份");
    }

    /**
     * 获取数据库备份详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:backup:query')")
    @GetMapping(value = "/{backupId}")
    public AjaxResult getInfo(@PathVariable("backupId") Long backupId)
    {
        return AjaxResult.success(databaseBackupService.selectDatabaseBackupById(backupId));
    }

    /**
     * 获取数据库的备份历史
     */
    @PreAuthorize("@ss.hasPermi('system:backup:query')")
    @GetMapping(value = "/history/{dbId}")
    public AjaxResult getBackupHistory(@PathVariable("dbId") Long dbId)
    {
        return AjaxResult.success(databaseBackupService.selectDatabaseBackupByDbId(dbId));
    }
    
    /**
     * 修改数据库备份
     */
    @PreAuthorize("@ss.hasPermi('system:backup:edit')")
    @Log(title = "数据库备份", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DatabaseBackup databaseBackup)
    {
        return toAjax(databaseBackupService.updateDatabaseBackup(databaseBackup));
    }

    /**
     * 删除数据库备份
     */
    @PreAuthorize("@ss.hasPermi('system:backup:remove')")
    @Log(title = "数据库备份", businessType = BusinessType.DELETE)
    @DeleteMapping("/{backupIds}")
    public AjaxResult remove(@PathVariable Long[] backupIds)
    {
        return toAjax(databaseBackupService.deleteDatabaseBackupByIds(backupIds));
    }
} 